﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cepheus;
using System.Drawing.Drawing2D;
using BinaryClockScreensaver.Properties;
using BinaryClockScreensaverCore;

namespace BinaryClockScreensaver
{
    public partial class FormScreensaver : Form
    {
        Point? lastMousePosition;

        DeviceChangedAgent deviceChangedAgent;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams p = base.CreateParams;
                p.ClassStyle += 0x0200/*CS_NOCLOSE*/;
                return p;
            }
        }

        public FormScreensaver()
        {
            InitializeComponent();

            int x = RandomNumbers.Next(0, this.Width - pictureBox1.Width);
            pictureBox1.Location = new Point(x, -(pictureBox1.Height + 100));

            int y = RandomNumbers.Next(0, this.Height - pictureBox2.Height);
            pictureBox2.Location = new Point(-(pictureBox2.Width + 100), y);

#if DEBUG
            timer1.Interval = RandomNumbers.Next(3000, 7000);
#else
            timer1.Interval = RandomNumbers.Next(7000, 30000);
#endif

            //deviceChangedAgent = new DeviceChangedAgent(this);
            //deviceChangedAgent.DeviceChanged += deviceChangedAgent_DeviceChanged;
        }

        //void deviceChangedAgent_DeviceChanged(object sender, DeviceChangedEventArgs e)
        //{
        //    if (e.DeviceChangedAction == DeviceChangedAction.Arrival)
        //    {
        //        Close();
        //    }
        //}

        private void binaryClock1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!lastMousePosition.HasValue)
            {
                lastMousePosition = e.Location;
            }
            else if (Math.Abs(lastMousePosition.Value.X - e.X) > 5 || Math.Abs(lastMousePosition.Value.Y - e.Y) > 5)
            {
                Close();
            }
        }

        private void FormSub_Load(object sender, EventArgs e)
        {
            binaryClock1.Start();
            timer1.Start();
        }

        private void binaryClock1_KeyDown(object sender, KeyEventArgs e)
        {
            Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            binaryClock1.Stop();

#if DEBUG
            int i = RandomNumbers.Next(3000, 7000);
#else
            int i = RandomNumbers.Next(7000, 30000);
#endif

            if (i % 2 == 0)
                timerLabel1.Start();
            else
                timerLabel2.Start();

            timer1.Interval = i;
        }

        private void timerLabel1_Tick(object sender, EventArgs e)
        {
            int step = 40;
            pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + step);

            if (pictureBox1.Top > this.Height)
            {
                binaryClock1.Start();
                timerLabel1.Stop();

                int x = RandomNumbers.Next(0, this.Width - pictureBox1.Width);
                pictureBox1.Location = new Point(x, -(pictureBox1.Height + 100));
            }
        }

        private void timerLabel2_Tick(object sender, EventArgs e)
        {
            int step = 40;
            pictureBox2.Location = new Point(pictureBox2.Location.X + step, pictureBox2.Location.Y);

            if (pictureBox2.Left > this.Width)
            {
                binaryClock1.Start();
                timerLabel2.Stop();

                int y = RandomNumbers.Next(0, this.Height - pictureBox2.Height);
                pictureBox2.Location = new Point(-(pictureBox2.Width + 100), y);
            }
        }
    }
}
